import axios from 'axios'
// import Number from 'less/lib/less/functions/number'
import qs from 'Qs'
// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  timeout: 5000, // request timeout
  paramsSerializer: function (params) {
    return qs.stringify(params, {arrayFormat: 'brackets'})
  },
  transformRequest: [function (data, headers) {
    // 对 data 进行任意转换处理
    return qs.stringify(data)
  }]
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    config.headers['content-type'] = 'application/x-www-form-urlencoded'
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    console.log(response.data.code)
    const res = response.data

    // if the custom code is not 20000, it is judged as an error.
    if (res.code === 0) {
      return Promise.reject(res.code)
    } else {
      console.log(res)
      return res
    }
  },
  error => {
    console.log('err' + error) // for debug

    return Promise.reject(error)
  }
)
export default service
